/*************************************************************************
	> File Name: thread.hpp
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023/10/01 22:36
 ************************************************************************/

#pragma once

#include <iostream>
#include <string>
#include <cstring>
#include <pthread.h>

typedef void*(*func_t)(void*);

const int NAME_SIZE = 64;

class ThreadData
{
public:
    void* _args;
    std::string _name;
};

class Thread
{
public:
    Thread (int num, func_t callback, void* args):_func(callback)
    {
        char threadname[NAME_SIZE];
        snprintf(threadname, sizeof threadname, "%s-%d", "Thead", num);
        _name = threadname;
        _tdata._args = args;
        _tdata._name = _name;
    }

    void start()
    {
        pthread_create(&_tid, nullptr, _func, (void*)&_tdata);
    }

    void join()
    {
        pthread_join(_tid, nullptr);
    }

    std::string name()
    {
        return _name;
    }

private:
    pthread_t _tid;
    std::string _name;
    func_t _func;
    ThreadData _tdata;
};
